package edu.ecnu.yt.spring.di.conditional;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration
@PropertySource("classpath:edu/ecnu/yt/spring/conditional/myEnv.properties")
public class ConditionalConfig {
	
	@Bean(name="testBean")
	// 检查环境变量是否存在myEnv且值为myEnvName，同时bean的Name是否是testBean，
	// 符合条件才创建testBean
	@Conditional(CheckMyEnvCondition.class)
	public TestBean testBean() {
		return new TestBean();
	}
	
	// 用于测试占位符。
	@Bean(name="testBeanMsg")
	public TestBean testBeanMsg(@Value("${myEnv.name}") String msg) {
		return new TestBean(msg);
	}
	
	@Bean
	public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}
	
}
